/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.service.user.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.validator.Assert;
import io.renren.modules.app.dao.user.UserDao;
import io.renren.modules.app.entity.user.UserEntity;
import io.renren.modules.app.form.LoginForm;
import io.renren.modules.app.service.user.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

	@Override
	public UserEntity queryByMobile(String mobile) {
		return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("mobile", mobile));
	}

	@Override
	public long login(LoginForm form) {

		UserEntity user =  baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("account_name",form.getMobile()));
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
			throw new RRException("手机号或密码错误");
		}

		return user.getId();
	}

	@Override
	public Map<String, Object> login(Map<String, Object> map){
		//创建返回map
		Map<String,Object> result = new HashMap<>();
		//根据用户名查询用户
		// 查询条件对象
//		QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
//		// 构建查询对象 QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
//		wrapper.eq("id", 1); // 查询  删除  更新
//
//		UserEntity userEntity = new UserEntity();
//		userEntity.setMobile("234");
//
//		baseMapper.insert(userEntity);
//
//		wrapper.eq("mobile", "234");
//		UserEntity userEntity1 = baseMapper.selectOne(wrapper);
//		baseMapper.selectById(1);
//		List<UserEntity> userEntities = baseMapper.selectList(wrapper);
//		baseMapper.deleteById(1);
//		baseMapper.delete(wrapper);
//		// userEntity id 不能为空
//		baseMapper.updateById(userEntity);
//
//
//		// 使用 baseMapper
//		 baseMapper.delete(wrapper);
//		// select * from user where id = 1;
//
//		"where id = 1 and name =222";
//		wrapper.eq("id", 1)
//				.or()
//				.eq("name", 2);
		UserEntity user = baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("account_name",map.get("loginAccountName").toString()));

		// update set id = 1 form user where id = 1;
//		wrapper.eq("id", user.getId());
//		baseMapper.update(user, wrapper);这两个要注掉  会报错那这个

		//用户为空代表该手机号未注册
		if(user == null){
			result.put("code",-100);
			result.put("msg","该手机号未注册");
			return result;
		}
		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(map.get("loginPassword").toString()))){
			result.put("code",-101);
			result.put("msg","密码错误");
			return result;
		}
		//手机号和密码输入正确
		result.put("code",200);
		result.put("msg","登录成功");
		result.put("userId",user.getId());
		result.put("companyUser",user);
		return result;
	}

	@Override
	public UserEntity getByUserId(Integer userId) {

		return baseMapper.selectByUserId(userId);
	}

	@Override
	public UserEntity getUserInfoById(Integer userId) {
		return baseMapper.selectById(userId);
	}
}